package com.seemsea.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *前端控制器工具类
 *
 *@author chirowong
 *@version 1.0 2011-06-02
 *@since 1.0
*/
public class ControllerUtils
{

	/**
	 * 返回json
	 * @param response
	 * @param result
	 */
	public static void responseClientWithJson(HttpServletResponse response, String result) {
		response.setContentType("text/json; charset=utf-8");
		responseClient(response, result);
	}
	
	/**
	 * @param request
	 * @param response
	 * @param result
	 */
	public static void responseClientWithJsonNoCache(HttpServletRequest request, HttpServletResponse response, String result) {
			setNoCache(request, response);
			responseClientWithJson(response, result);
	}
	
	/**
	 * @param response
	 * @param result
	 */
	public static void responseClientWithText(HttpServletResponse response, String result) {
		response.setContentType("text/txt; charset=utf-8");
		responseClient(response, result);
	}
	
	/**
	 * @param request
	 * @param response
	 * @param result
	 */
	public static void responseClientWithTextNoCache(HttpServletRequest request, HttpServletResponse response, String result) {
		setNoCache(request, response);
		responseClientWithText(response, result);
	}

	/**
	 * @param response
	 * @param result
	 */
	public static void responseClientWithHtml(HttpServletResponse response, String result) {
		response.setContentType("text/html; charset=utf-8");
		responseClient(response, result);
	}
	
	/**
	 * @param request
	 * @param response
	 * @param result
	 */
	public static void responseClientWithHtml(HttpServletRequest request, HttpServletResponse response, String result) {
		setNoCache(request, response);
		responseClientWithHtml(response, result);
	}
	
	/**
	 * @param response
	 * @param result
	 */
	public static void responseClientWithXML(HttpServletResponse response, String result) {
		response.setContentType("text/xml; charset=utf-8");
		responseClient(response, result);
	}
	
	/**
	 * @param request
	 * @param response
	 * @param result
	 */
	public static void responseClientWithXMLNoCache(HttpServletRequest request, HttpServletResponse response, String result) {
		setNoCache(request, response);
		responseClientWithXML(response, result);
	}
	
	public static void responseWithRelativePath(HttpServletResponse response, String relativePath) {
		responseWithAbsoluteFilePath(response, System.getProperty("fews.root") + File.separator + relativePath);
	}

	public static void responseWithAbsoluteFilePath(
			HttpServletResponse response, String absolutePath) {
		response.setContentType("text/txt; charset=utf-8");
		try {
			PrintWriter printWriter = response.getWriter();
			InputStreamReader in = new InputStreamReader(new FileInputStream(new File(absolutePath)), "utf-8");
			char[] buffer = new char[1024];
			int charRead;
			while ((charRead = in.read(buffer)) != -1) {
				printWriter.write(new String(buffer,0, charRead));
			}
			in.close();
			printWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void responseClient(HttpServletResponse response, String result) {
		try {
			PrintWriter writer = response.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void setNoCache(HttpServletRequest request, HttpServletResponse response) {
		if(request.getProtocol().equals("HTTP/1.0"))
		   response.setHeader("Pragma","no-cache");
		if(request.getProtocol().equals("HTTP/1.1"))
		   response.setHeader("Cache-Control","no-cache");
		response.setDateHeader("Expires",0);
	}
	
}
